﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;

namespace XchangeStreamer.OutputSdk.Internal
{
    internal class Factory
    {
        private static readonly string path = "XchangeStreamer.OutputSdk.Collections";
        private static readonly string className = "XchangeStreamer.OutputSdk.Collections.ReadOnlyObservableCollection<T>";
        private static Assembly assembly;
        
        static Factory()
        {
            try
            {
                assembly = Assembly.Load(path);
            }
            catch
            {
                throw;
            }
        }

        public static IList<T> Create<T>(ObservableCollection<T> list)
        {
            try
            {
                Type type = assembly.GetExportedTypes()[0];
                Type[] typeArgs = { typeof(T) };
                Type constructed = type.MakeGenericType(typeArgs);
                return (IList<T>)Activator.CreateInstance(constructed, list);
            }
            catch
            {
                throw;
            }
        }
    }
}
